package com.example.backend.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.common.api.R;
import com.example.backend.common.annotation.OperationLog;
import com.example.backend.dto.RoleDTO;
import com.example.backend.entity.Role;
import com.example.backend.service.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理控制器
 */
@RestController
@RequestMapping("/admin/role")
@RequiredArgsConstructor
public class RoleController {
    
    private final RoleService roleService;
    
    /**
     * 分页查询角色列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('system:role:list')")
    public R<Page<Role>> list(@RequestParam(defaultValue = "1") Integer current,
                             @RequestParam(defaultValue = "10") Integer size,
                             String keyword) {
        Page<Role> page = roleService.page(new Page<>(current, size),
                new LambdaQueryWrapper<Role>()
                        .like(keyword != null, Role::getName, keyword)
                        .or()
                        .like(keyword != null, Role::getCode, keyword)
                        .orderByDesc(Role::getCreateTime));
        return R.ok(page);
    }
    
    /**
     * 获取所有角色
     */
    @GetMapping("/all")
    @PreAuthorize("hasAuthority('system:role:list')")
    public R<List<Role>> all() {
        List<Role> roles = roleService.list(new LambdaQueryWrapper<Role>()
                .eq(Role::getStatus, 1)
                .orderByAsc(Role::getSort)
                .orderByDesc(Role::getCreateTime));
        return R.ok(roles);
    }
    
    /**
     * 获取用户角色
     */
    @GetMapping("/user/{userId}")
    @PreAuthorize("hasAuthority('system:role:list')")
    public R<List<Role>> getUserRoles(@PathVariable Long userId) {
        return R.ok(roleService.getUserRoles(userId));
    }
    
    /**
     * 创建角色
     */
    @OperationLog(module = "角色管理", type = "新增", description = "新增角色")
    @PostMapping
    @PreAuthorize("hasAuthority('system:role:add')")
    public R<Void> createRole(@Validated(RoleDTO.Create.class) @RequestBody RoleDTO roleDTO) {
        roleService.createRole(roleDTO);
        return R.ok();
    }
    
    /**
     * 更新角色
     */
    @OperationLog(module = "角色管理", type = "修改", description = "修改角色")
    @PutMapping
    @PreAuthorize("hasAuthority('system:role:edit')")
    public R<Void> updateRole(@Validated(RoleDTO.Update.class) @RequestBody RoleDTO roleDTO) {
        roleService.updateRole(roleDTO);
        return R.ok();
    }
    
    /**
     * 删除角色
     */
    @OperationLog(module = "角色管理", type = "删除", description = "删除角色")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('system:role:delete')")
    public R<Void> deleteRole(@PathVariable Long id) {
        roleService.deleteRole(id);
        return R.ok();
    }
    
    /**
     * 更新角色权限
     */
    @OperationLog(module = "角色管理", type = "修改", description = "分配角色权限")
    @PutMapping("/{roleId}/permissions")
    @PreAuthorize("hasAuthority('system:role:permission')")
    public R<Void> updateRolePermissions(@PathVariable Long roleId, @RequestBody List<Long> permissionIds) {
        roleService.updateRolePermissions(roleId, permissionIds);
        return R.ok();
    }
    
    /**
     * 获取角色权限
     */
    @GetMapping("/{roleId}/permissions")
    @PreAuthorize("hasAuthority('system:role:list')")
    public R<List<Long>> getRolePermissions(@PathVariable Long roleId) {
        return R.ok(roleService.getRolePermissionIds(roleId));
    }
} 